package handler

import (
	"github.com/gin-gonic/gin"
	"micros/api/params"
	"micros/golbal"
	"micros/model"
	"net/http"
)

func AddBlack(c *gin.Context) {

	add := params.AddBlack{}
	if err := c.ShouldBind(&add); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	black := model.Black{

		UserId:   add.UserId,
		FriendId: add.FriendId,
	}

	golbal.DB.Debug().Create(&black)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "黑名单添加成功",
	})
}

func BlackList(c *gin.Context) {
	list := params.BlackList{}
	if err := c.ShouldBind(&list); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var user []model.BlackList

	golbal.DB.Model(&model.User{}).Select("users.*,blacks.friend_id").
		Joins("join blacks on blacks.user_id=users.id").Where("blacks.friend_id").
		Where("blacks.friend_id", list.FriendId).Scan(&user)

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "黑名单列表查询成功",
		"data": user,
	})
}
